﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using BEPUphysics.Entities;

namespace Asplode.Renderer
{
    public class AsplodeRendererEventHandler
    {

        public AsplodeRenderer Renderer { get; set; }

        public AsplodeRendererEventHandler(AsplodeRenderer renderer)
        {
            Renderer = renderer;
        }

        public void subscribe(EventDispatcher EventDispatcher)
        {
            EventDispatcher.BoxSpawn += new EventDispatcher.BoxSpawnHandler(EventDispatcher_BoxSpawn);
            EventDispatcher.HumanSpawn += new EventDispatcher.HumanSpawnHandler(EventDispatcher_HumanSpawn);
            EventDispatcher.RemoveEntity += new EventDispatcher.RemoveEntityHandler(EventDispatcher_RemoveEntity);
            EventDispatcher.ZombieSpawn += new EventDispatcher.ZombieSpawnHandler(EventDispatcher_ZombieSpawn);

        }

        void EventDispatcher_BoxSpawn(Box Box)
        {
            Model model = Renderer.game.Content.Load<Model>("Models\\cube");
            RenderPair rp = new RenderPair(Box, model);
            rp.transform = Matrix.CreateScale(Box.width, Box.height, Box.length);
            Renderer.models.Add(rp);
        }

        void EventDispatcher_HumanSpawn(Entity entity)
        {
            Model model = Renderer.game.Content.Load<Model>("Models\\character");
            Renderer.models.Add(new RenderPair(entity, model));
        }

        void EventDispatcher_RemoveEntity(Entity entity)
        {
            foreach (RenderPair rp in Renderer.models)
            {
                if (rp.Entity == entity)
                {
                    Renderer.models.Remove(rp);
                    return;
                }
            }
        }

        void EventDispatcher_ZombieSpawn(Entity entity)
        {
            Model model = Renderer.game.Content.Load<Model>("Models\\Zombie");
            Renderer.models.Add(new RenderPair(entity, model));
        }
    }
}
